import math from "../math";

let result = new Object();

result.toWebview = function (src) {
	src=encodeURIComponent(src);
	uni.navigateTo({
		url: '/pages/web-view-page/web-view-page?src=' + src
	});
}

/**
 * 判断内容是否为空
 * @return {boolean} 布尔类型：true代表字段为空，false 代表字段不为空
 */
result.isEmpty = function (varr) {
	if(varr===0){
		return false;
	}
	if (varr == null || varr === "") {
		return true;
	}
	return false;
}


/**
 * 生成uuid
 * 默认长度为32位
 * @param {Object} len 长度
 */
result.createUUID = function (len) {
	len = len || 32;
	var $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var maxPos = $chars.length;
	var pwd = '';
	for (var i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
}

/**
 * 前往跳转
 * @param {Object} path
 */
result.navTo = function (path) {
	uni.navigateTo({
		url: path
	})
}
/**
 * 关闭当前页面，跳转到应用内的某个页面
 */
result.reTo = function (path) {
	uni.redirectTo({
		url: path
	})
}

result.reLaunch = function (path) {
	uni.reLaunch({
		url: path
	})
}

/**
 * @param {Object} json
 */
result.newJSON = function (json) {
	return JSON.parse(JSON.stringify(json));
}


result.pxToRpx = function (px) {
	// return px * 750 / wx.getSystemInfoSync().windowWidth;
	return math.evaluate(`${px} * 750 / ${uni.getSystemInfoSync().windowWidth}`);
};


result.rpxToPx = function (rpx) {
	// return rpx / 750 * wx.getSystemInfoSync().windowWidth;
	return math.evaluate(`${rpx} / 750 * ${uni.getSystemInfoSync().windowWidth}`);
};

/**
 * 获取系统信息
 */
result.getSystemInfo = function () {
	var systemInfo = uni.getSystemInfoSync();
	return systemInfo;
}

result.readableDistance = function (m) {
	var m2 = m + "";
	m2 = m2.substring(m2.indexOf(".") + 1);

	var result = {
		unit: 'm',
		num: m2
	};


	if (m >= 1000) {
		var km = (m / 1000);
		var km2 = km.toFixed(2) + "";
		var aa = km2.substring(km2.indexOf(".") + 1);
		var bb = km2.substring(km2.indexOf(".") + 2);
		if (aa <= 10) {
			km2 = km2.substring(0, km2.indexOf("."));
		}
		var result = {
			unit: 'km',
			num: km2
		};
	}
	return result;
}

/**
 * 系统信息
 */
result.systemInfo = result.getSystemInfo();

/**
 * 给json按照key的字母顺序排序，并返回排序后的json
 */
result.sortJSONByKey = function (json) {
	var arr = new Array();
	for (var key in json) {
		arr.push(key);
	}
	arr.sort();
	var newJson = new Object();
	for (var i = 0; i < arr.length; i++) {
		newJson[arr[i]] = json[arr[i]];
	}
	return newJson;
}

/**
 * 单词首字母大写
 */
result.firstLetterUpper = function (word) {
	return word.substring(0, 1).toUpperCase() + word.substring(1);
}


export default result;
